package com.yaozheng.vocationaltraining.fragment;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.MemoryWealthAdapter;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IItemView;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.iview.IVideoCoursewareView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.CoursewareDetailService;
import com.yaozheng.vocationaltraining.service.MemoryWealthService;
import com.yaozheng.vocationaltraining.service.impl.CoursewareDetailServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.MemoryWealthServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 记忆财富(你该复习的课程)
 *
 * @author Administrator
 */
@EFragment(R.layout.activity_news_list)
public class MemoryWealthFragment extends BaseFragment implements
        ITokenBaseView, SwipeRefreshLayout.OnRefreshListener, ILoadMoreView,
        IItemView {
    @ViewById
    ListView listView;

    @ViewById
    TextView headTitleText;

    @Bean(MemoryWealthServiceImpl.class)
    MemoryWealthService memoryWealthService;

    MemoryWealthAdapter memoryWealthAdapter;
    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    ListViewLoadMoreManage listViewLoadMoreManage;
    @Bean(CoursewareDetailServiceImpl.class)
    CoursewareDetailService coursewareDetailService;

    int lastId;

    @AfterViews
    public void initView() {
        headTitleText.setText("你该复习的课程");
        memoryWealthService.init(this);

        listViewLoadMoreManage = new ListViewLoadMoreManage(this,
                getBaseActivity(), listView);
        swipeRefreshLayout.setOnRefreshListener(this);
        coursewareDetailService.init(new IVideoCoursewareView() {
            @Override
            public void responseSuccess(JSONObject jsonObject) {
                getCourserDetailSuccess(jsonObject);
            }

            @Override
            public void responseError(String error) {
                getCourserDetailError(error);
            }

            @Override
            public boolean isResponseResult() {
                return MemoryWealthFragment.this.isResponseResult();
            }

            @Override
            public void tokenFail() {
                MemoryWealthFragment.this.tokenFail();
            }

            @Override
            public String getToken() {
                return MemoryWealthFragment.this.getToken();
            }
        });

    }

    @Override
    public void onResume() {
//        swipeRefreshLayout.postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                onRefresh();
//
//            }
//        }, 500);
        onRefresh();
        super.onResume();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        memoryWealthService.list(lastId);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        JSONArray coursesDataList = TypeUtils.getJsonArray(jsonObject, "data",
                "courses");

        if (coursesDataList != null) {
            if (memoryWealthAdapter == null) {
                memoryWealthAdapter = new MemoryWealthAdapter(
                        getBaseActivity(), coursesDataList, this);
                listView.setAdapter(memoryWealthAdapter);

            } else {
                if (lastId == 0) {
                    memoryWealthAdapter.getDataList().clear();
                }
                memoryWealthAdapter.getDataList().addAll(coursesDataList);
                memoryWealthAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

    @UiThread
    @Override
    public void responseError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    @Override
    public boolean isResponseResult() {
        return isAdded();
    }

    @Override
    public void loadMoreData() {
        if (memoryWealthAdapter != null) {
            int lastId = memoryWealthAdapter.getLastId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }

    @Override
    public void itemClick(int position) {
        if (memoryWealthAdapter != null) {
            JSONObject itemData = TypeUtils.getJsonObject(
                    memoryWealthAdapter.getDataList(), position);
            showOperatingProgressDialog("加载中。");
            coursewareDetailService.getCourse(TypeUtils.getJsonInteger(
                    itemData, "courseId"));
        }
    }

    @UiThread
    public void getCourserDetailSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        JSONObject courserJsonObject = TypeUtils.getJsonObject(jsonObject,
                "data", "course");
        Intent intent = new Intent();
        intent.putExtra("title", "你该复习的课程");
        intent.putExtra("courserJsonStr", courserJsonObject.toString());
        intent.putExtra("isLoadNew", false);
        if (!"".equals(TypeUtils.getJsonString(
                TypeUtils.getJsonObject(courserJsonObject, "resources"),
                "video", ""))) {
            intent.setClassName(getContext(),
                    Constants.VIDEO_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);

        } else {
            intent.setClassName(getContext(),
                    Constants.IMAGE_LIST_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);
        }
        startActivity(intent);
    }

    @UiThread
    public void getCourserDetailError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    public void onDestroyView() {
        memoryWealthAdapter = null;
        super.onDestroyView();
    }
}
